//
//  IssueViewController.m
//  Blank
//
//  Created by 马博 on 15/9/28.
//  Copyright © 2015年 dllo. All rights reserved.
//

#import "IssueViewController.h"
#import "IssueCell.h"
#import "DataBaseHandle.h"/* 数据库头文件 */
#import "CollectModel.h"

#define windth [UIScreen mainScreen].bounds.size.width / 375
#define heightt ([UIScreen mainScreen].bounds.size.height > 568 ? [UIScreen mainScreen].bounds.size.height : 568) / 667


@interface IssueViewController ()<UIWebViewDelegate>

@property (nonatomic, strong) UIWebView *webView;
@property (nonatomic, strong) UIButton *rightButton;
@property (nonatomic ,strong) UIActivityIndicatorView  *activityIndicator;


@end

@implementation IssueViewController


- (void)viewWillAppear:(BOOL)animated
{
    //button的点击事件的判断
    if (self.count == 1) {
        [self.rightButton setImage:[UIImage imageNamed:@"iconfont-shoucang.png"] forState:UIControlStateNormal];
    }else{
        [self.rightButton setImage:[UIImage imageNamed:@"iconfont-favorite.png"] forState:UIControlStateNormal];
    }

}


- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    //得出路径
    NSLog(@"AAAAAAAAAAAAAAAAAA   %@", [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)lastObject]);
    
    UIButton *buttonBack = [UIButton buttonWithType:UIButtonTypeCustom];
    [buttonBack setImage:[UIImage imageNamed:@"iconfont-fanhui"] forState:UIControlStateNormal];
    buttonBack.frame = CGRectMake(0, 0, 20, 20);
    
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:buttonBack];
    
    [buttonBack addTarget:self action:@selector(back:) forControlEvents:UIControlEventTouchUpInside];
    
    //放在rightItem上的view
    UIView *rightBarView = [[UIView alloc] initWithFrame:CGRectMake(windth * 0, 0, windth * 60, heightt * 30)];
    //放在view上的button
    self.rightButton = [UIButton buttonWithType:UIButtonTypeCustom];
    self.rightButton.frame = CGRectMake(windth * 30, 0, windth * 30, heightt * 30);
   
    //为button添加点击事件
    [self.rightButton addTarget:self action:@selector(rightbutton1:) forControlEvents:UIControlEventTouchUpInside];
    //将button加到view上
    [rightBarView addSubview:self.rightButton];
    //将view放到item上
    UIBarButtonItem *right = [[UIBarButtonItem alloc] initWithCustomView:rightBarView];
    //将item恒等rightitem
    self.navigationItem.rightBarButtonItem = right;
    
    //判断收藏
    [self Collect];
    //web页的加载
    [self webViewSubView];
    
}

- (void)back:(UIButton *)button
{
    [self.navigationController popToRootViewControllerAnimated:YES];
}

#pragma mark -- 判断是否已经收藏
- (void)Collect
{
    /* 打开数据库 */
    [[DataBaseHandle shareDataBaseHandle] open];
    NSArray *collectArr = [NSArray array];
    
    /* 创建表单 */
    collectArr = [[DataBaseHandle shareDataBaseHandle] selectCollect];
    CollectModel *collect = [[CollectModel alloc] init];
    for (int i = 0; i < collectArr.count; i++) {
        collect = [collectArr objectAtIndex:i];
        if ([collect.title isEqualToString:self.webTitle]) {
            self.count = 1;
            break;
        }else{
            self.count = 0;
        }
    }
    /* 关闭数据库 */
    [[DataBaseHandle shareDataBaseHandle] close];
}


- (void)webViewSubView
{
    self.webView = [[UIWebView alloc] initWithFrame:CGRectMake(windth * 0, 0, windth * 375, self.view.frame.size.height)];
    NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:self.url]];
    [self.webView loadRequest:request];
    [self.view addSubview:self.webView];
    self.webView.delegate = self;

}

- (void)rightbutton1:(UIButton *)rightbutton
{
    CollectModel *collect = [[CollectModel alloc] init];
    collect.title = self.webTitle;
    collect.url = self.url;
    
    /* 打开数据库 */
    [[DataBaseHandle shareDataBaseHandle] open];
    
    /* 创建表单 */
    [[DataBaseHandle shareDataBaseHandle] createTable];
    if (self.count == 0) {
        
        [[DataBaseHandle shareDataBaseHandle] insertCollert:collect];
        self.count = 1;
    }else{
        [[DataBaseHandle shareDataBaseHandle] deleteCollect:self.webTitle];
        self.count = 0;
    }
    [[DataBaseHandle shareDataBaseHandle]  close];
    
    if (self.count == 1) {
        [self.rightButton setImage:[UIImage imageNamed:@"iconfont-shoucang.png"] forState:UIControlStateNormal];
        UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"收藏成功!" message:@"请到收藏列表查看" preferredStyle:UIAlertControllerStyleAlert];
        /* 添加 action */
        UIAlertAction *action1 = [UIAlertAction actionWithTitle:@"好的!" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
        }];
        [alert addAction:action1];
        /*  VC 模态推出alert 界面 */
        [self presentViewController:alert animated:YES completion:^{
        }];
    }else{
        [self.rightButton setImage:[UIImage imageNamed:@"iconfont-favorite.png"] forState:UIControlStateNormal];
        UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"已取消该收藏" message:@"" preferredStyle:UIAlertControllerStyleAlert];
        /* 添加 action */
        UIAlertAction *action1 = [UIAlertAction actionWithTitle:@"我知道了" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
        }];
        [alert addAction:action1];
        /* VC 模态推出alert 界面  */
        [self presentViewController:alert animated:YES completion:^{
        }];
    }
}

/* 小菊花 */
- (void)webViewDidStartLoad:(UIWebView *)webView{
    
    UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 50 * windth , 50 * heightt)];
    view.center = CGPointMake(windth * 375 / 2.0, heightt * 667 / 2.0 - heightt * 50);
    [view setTag:100];
    [view setBackgroundColor:[UIColor blackColor]];
    [view setAlpha:0.5];
    [self.view addSubview:view];

    self.activityIndicator = [[UIActivityIndicatorView alloc] initWithFrame:CGRectMake(0, 0, 32 * windth , heightt * 32)];
    _activityIndicator.center = CGPointMake(windth*375 / 2.0, heightt * 667/ 2.0 - heightt * 50);
    [_activityIndicator setActivityIndicatorViewStyle:UIActivityIndicatorViewStyleWhite];
    [self.view addSubview:_activityIndicator];
    
    [_activityIndicator startAnimating];
}

- (void)webViewDidFinishLoad:(UIWebView *)webView
{
    
    if ([NightModel getColorModel]) {
        
        [webView stringByEvaluatingJavaScriptFromString:@"document.getElementsByTagName('body')[0].style.webkitTextFillColor= 'white'"];
        //页面背景色
        [webView stringByEvaluatingJavaScriptFromString:@"document.getElementsByTagName('body')[0].style.background='gray'"];
    }
    
    [_activityIndicator stopAnimating];
    UIView *view = (UIView *)[self.view viewWithTag:100];
    [view removeFromSuperview];
}

- (void)webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error
{
    [_activityIndicator stopAnimating];
    UIView *view = (UIView *)[self.view viewWithTag:100];
    [view removeFromSuperview];
    
    [webView reload];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
